package com.briup.web.controller;

import com.briup.bean.User;
import com.briup.service.IUserService;
import jdk.nashorn.internal.objects.NativeUint8Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private IUserService userService;
    @GetMapping(value = {"/toLogin","/login"})
    public String toLogin( ){
        return "login";
    }

    @PostMapping(value = "/login")
    public String LoginCheck(HttpServletRequest request,Model model) throws Exception {
        //1.获取登录用户名
        String username = request.getParameter("loginName");
        User user = userService.login(username, DigestUtils.md5DigestAsHex(request.getParameter("password").getBytes()));
        if(user == null){
            model.addAttribute("msg","账号或密码错误！");
            return "login";
        }
        //2.获取或创建session对象
        HttpSession session = request.getSession();
        //3.session对象中保存用户登录信息
        session.setAttribute("user", user);
        //System.out.println(pwd+username);

        return "redirect:/index";
    }

}
